import torch.nn
import torchvision
import torch.nn.functional as F
import numpy as np
import matplotlib.pyplot as plt

# train_set = torchvision.datasets.MNIST(root=r'../dataset/mnist', train=True, download=True)
# test_set = torchvision.datasets.MNIST(root=r'../dataset/mnist', train=False, download=True)
# train_set = torchvision.datasets.CIFAR10(root=r'../dataset/cifar10', train=True, download=True)
# test_set = torchvision.datasets.CIFAR10(root=r'../dataset/cifar10', train=False, download=True)
# Prepare dataset
x_data = torch.tensor([[1.0], [2.0], [3.0]])
y_data = torch.tensor([[0], [0], [1]])
y_data = y_data.to(torch.float32)


# Design model using Class
class LogisticRegressionModel(torch.nn.Module):
    def __init__(self):
        super(LogisticRegressionModel, self).__init__()
        # Linear transformation
        self.linear = torch.nn.Linear(1, 1)

    def forward(self, x):
        y_pred = F.sigmoid(self.linear(x))
        return y_pred


model = LogisticRegressionModel()
# Construct loss and optimizer
criterion = torch.nn.BCELoss(size_average=False)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
# Training cycle
for epoch in range(100000):
    # Forward
    y_pred = model(x_data)
    y_pred = y_pred.to(torch.float32)
    loss = criterion(y_pred, y_data)
    print(epoch, loss.item())
    # Backward
    optimizer.zero_grad()
    loss.backward()
    # Update
    optimizer.step()
print('w=', model.linear.weight.item())
print('b=', model.linear.bias.item())
x = np.linspace(0, 10, 200)
x_t = torch.Tensor(x).view((200, 1))
y_t = model(x_t)
y = y_t.data.numpy()
plt.plot(x, y)
plt.plot([0, 10], [0.5, 0.5], c='r')
plt.xlabel('Hours')
plt.ylabel('Probability of Pass')
plt.grid()
plt.show()
